package composelearn

import "fmt"

//go是不支持继承的，但可以通过组合，尽可能达到类似的效果

type Teacher struct {
	Name  string
	Age   int
	Title string
}

func (t Teacher) teacherInfo() {
	fmt.Printf("姓名：%s, 年龄：%d, 职称：%s", t.Name, t.Age, t.Title)
}

type Course struct {
	Teacher Teacher //如果讲师的信息比较多，就不能用string,怎么办？可以定义为另一个结构体
	Name    string
	Price   int
	Url     string
}

func (c Course) courseInfo() {
	fmt.Printf("课程名：%s, 价格：%d,讲师信息：%s %d %s \n", c.Name, c.Price, c.Teacher.Name, c.Teacher.Age, c.Teacher.Title)
	c.Teacher.teacherInfo()
}

func ComposeLearn() {
	t := Teacher{
		Name:  "bobby",
		Age:   18,
		Title: "程序员",
	}

	c := Course{
		Teacher: t,
		Name:    "Django",
		Price:   100,
		Url:     "http://www.imooc.com",
	}

	c.courseInfo()
}

//匿名嵌套
//Name冲突的时候，可以使用c.Teacher.Name明确
type Course2 struct {
	Teacher
	Name  string
	Price int
	Url   string
}

func (c Course2) courseInfo() {
	fmt.Printf("课程名：%s, 价格：%d,讲师信息：%s %d %s \n", c.Name, c.Price, c.Teacher.Name, c.Age, c.Title) //可以直接使用c.出teacher的内容
	c.teacherInfo()
}

func AnonyComposeLearn() {
	t := Teacher{
		Name:  "bobby",
		Age:   18,
		Title: "程序员",
	}

	c2 := Course2{
		Teacher: t,
		Name:    "Django",
		Price:   100,
		Url:     "http://www.imooc.com",
	}

	c2.courseInfo()
}
